local que_linghu = fk.CreateSkill {
    name = "que_linghu",
    anim_type = "control",
}
Fk:loadTranslationTable {
    ["que_linghu"] = "灵狐",
    [":que_linghu"] = "出牌阶段限一次，或当你每回合首次受到其他角色造成的伤害后，你可以弃置一张手牌并进行一次判定，根据花色，点数，颜色，类型和牌名相同的项数，执行对应效果的<a href='linghu_cifu'>赐福</a>。",
    ["linghu_cifu"] = "相同的项数为：<br>" ..
        "0.同一名其他角色失去一点体力并重置灵狐；<br>" ..
        "1.分别令两名角色执行弃牌和摸牌；<br>" ..
        "2.分别令两名角色回复体力和失去体力；<br>" ..
        "3.分别令两名角色增加和减少体力上限；<br>" ..
        "4.从三个同势力武将中获得一个技能；<br>" ..
        "然后你摸等X张牌(X为符合数，且至多为3)。",

    ["#que_linghu_ask"] = "灵狐：你可以展示一张手牌并进行判定，根据判定结果执行效果。",

    ["@que_linghu-turn"] = "灵狐",
}
local U = require "packages/utility/utility"
que_linghu:addEffect("active", {
    anim_type = "control",
    prompt = "#que_linghu_ask",
    card_num = 1,
    target_num = 0,
    can_use = function(self, player)
        return player:usedSkillTimes(que_linghu.name, Player.HistoryPhase) == 0 and player:getHandcardNum() > 0
    end,
    card_filter = function(self, player, to_select, selected)
        return #selected == 0 and table.contains(player:getCardIds("h"), to_select)
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        room:throwCard(effect.cards, que_linghu.name, player, player)
        room:delay(800)
        local judge = {
            who = player,
            reason = que_linghu.name,
            pattern = ".|.|.|.",
        }
        room:judge(judge)
        local x = 0
        if judge.card:getSuitString() == Fk:getCardById(effect.cards[1]):getSuitString() then
            x = x + 1
        else
            x = 0
        end
        if judge.card.number == Fk:getCardById(effect.cards[1]).number then
            x = x + 1
        end
        if judge.card.type == Fk:getCardById(effect.cards[1]).type then
            x = x + 1
        end
        if judge.card.name == Fk:getCardById(effect.cards[1]).name then
            x = x + 1
        end
        if judge.card.color == Fk:getCardById(effect.cards[1]).color then
            x = x + 1
        end
        room:setPlayerMark(player, "@que_linghu-turn", x)
        local yes = table.find(player:getCardIds("h"), function(id)
            return Fk:getCardById(id):getTypeString() == judge.card:getTypeString()
        end)
        if yes and player:hasSkill("que_jiangshen") and player:getMark("que_jiangshen_used") == 0 then
            if player.room:askToSkillInvoke(player, {
                    skill_name = "que_jiangshen",
                    prompt = "#que_jiangshen:::" .. judge.card:getTypeString()
                }) then
                room:addPlayerMark(player, "que_jiangshen_used", 1)
                local card = room:askToDiscard(player, {
                    skill_name = que_linghu.name,
                    cancelable = false,
                    pattern = ".|.|.|.|.|" .. judge.card:getTypeString(),
                    min_num = 1,
                    max_num = 1,
                    include_equip = false,
                })
                if #card > 0 then
                    local choices = { "符合项+1" }
                    if x > 0 then
                        table.insert(choices, "符合项-1")
                    end
                    if room:askToChoice(player, { choices = choices, skill_name = que_linghu.name, all_choices = choices }) == "符合项+1" then
                        x = x + 1
                        room:addPlayerMark(player, "@que_linghu-turn", 1)
                    else
                        x = x - 1
                        room:removePlayerMark(player, "@que_linghu-turn", 1)
                    end
                end
            end
        end
        if x == 0 then
            local targets = table.filter(room:getOtherPlayers(player), function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择一起失去体力的角色",
                cancelable = true,
            })[1]
            if to then
                room:loseHp(to, 1, que_linghu.name)
                room:loseHp(player, 1, que_linghu.name)
                player:setSkillUseHistory("que_linghu", 0, Player.HistoryPhase)
            end
        end
        if x > 0 then
            local targets = table.filter(room.alive_players, function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择摸牌的角色",
                cancelable = true,
            })[1]
            if to then
                to:drawCards(1, que_linghu.name)
            end
            local targets2 = table.filter(room.alive_players, function(p)
                return #p:getCardIds("hej") > 0
            end)
            local to2 = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets2,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择被弃牌的角色",
                cancelable = true,
            })[1]
            if to2 then
                local cid = room:askToChooseCard(player, {
                    target = to2,
                    flag = "hej",
                    skill_name = que_linghu.name,
                })
                room:throwCard(cid, que_linghu.name, to2, player)
            end
        end
        if x > 1 then
            local targets = table.filter(room.alive_players, function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择失去体力的角色",
                cancelable = true,
            })[1]
            if to then
                room:loseHp(to, 1, que_linghu.name)
            end
            local to2 = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择回复体力的角色",
                cancelable = true,
            })[1]
            if to2 then
                if to2:isWounded() then
                    room:recover {
                        who = to2,
                        num = 1,
                        recoverBy = player,
                        skillName = que_linghu.name
                    }
                end
            end
        end
        if x > 2 then
            local targets = table.filter(room.alive_players, function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择减少体力上限的角色",
                cancelable = true,
            })[1]
            if to then
                room:changeMaxHp(to, -1)
            end
            local to2 = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择增加体力上限的角色",
                cancelable = true,
            })[1]
            if to2 then
                room:changeMaxHp(to2, 1)
            end
        end
        if x > 3 then
            local blackgenerallist = { "que__xuanyin", "que__ailisha" }
            local generals, same_generals = {}, {}
            for _, general_name in ipairs(room.general_pile) do
                same_generals = Fk:getSameGenerals(general_name)
                table.insert(same_generals, general_name)
                same_generals = table.filter(same_generals, function(g_name)
                    local general = Fk.generals[g_name]
                    return general.kingdom == "que" or
                        general.subkingdom == "que" and not table.contains(blackgenerallist, general.name)
                end)
                if #same_generals > 0 then
                    table.insert(generals, table.random(same_generals))
                end
            end
            if #generals == 0 then return false end
            generals = table.random(generals, 3)

            local skills = {}
            for _, general_name in ipairs(generals) do
                local general = Fk.generals[general_name]
                local general_skills = {}
                for _, s in ipairs(general:getSkillNameList()) do
                    local skill = Fk.skills[s]
                    if not player:hasSkill(skill) and not (skill:hasTag(Skill.AttachedKingdom) and skill:hasTag(Skill.Limited) and not table.contains(skill:getSkeleton().attached_kingdom, player.kingdom)) then
                        table.insertIfNeed(general_skills, s)
                    end
                end
                table.insertIfNeed(skills, general_skills)
            end
            if #skills > 0 then
                local choices = U.askToChooseGeneralSkills(player, {
                    generals = generals,
                    skills = skills,
                    min_num = 1,
                    max_num = 1,
                    skill_name = que_linghu.name,
                    prompt = "灵狐：请选择要获得的技能",
                    cancelable = false
                })
                room:handleAddLoseSkills(player, table.concat(choices, "|"))
            end
        end
        if x > 0 and not player.dead then
            player:drawCards(math.min(x, 3), que_linghu.name)
        end
        room:setPlayerMark(player, "@que_linghu-turn", 0)
    end,
})

que_linghu:addEffect(fk.Damaged, {
    can_trigger = function(self, event, target, player, data)
        local on_damaged = player.room.logic:getActualDamageEvents(1, function(e)
            return e.data.to == player
        end, Player.HistoryTurn)
        return target == player and player:hasSkill(que_linghu.name) and data.from and data.from ~= player and
            player:getHandcardNum() > 0 and #on_damaged == 1 and on_damaged[1].data == data --首次受到伤害模板
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local card = room:askToCards(player, {
            min_num = 1,
            max_num = 1,
            include_equip = false,
            skill_name = que_linghu.name,
            prompt = "#que_linghu_ask",
            cancelable = true,
        })
        if #card > 0 then
            event:setCostData(self, { cards = card })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cid = Fk:getCardById(event:getCostData(self).cards[1])
        room:throwCard(cid, que_linghu.name, player, player)
        room:delay(800)
        local judge = {
            who = player,
            reason = self.name,
            pattern = ".|.|.|.",
        }
        room:judge(judge)
        local x = 0
        if judge.card:getSuitString() == cid:getSuitString() then
            x = x + 1
        else
            x = 0
        end
        if judge.card.number == cid.number then
            x = x + 1
        end
        if judge.card.type == cid.type then
            x = x + 1
        end
        if judge.card.name == cid.name then
            x = x + 1
        end
        if judge.card.color == cid.color then
            x = x + 1
        end
        room:setPlayerMark(player, "@que_linghu-turn", x)
        if x == 0 then
            local targets = table.filter(room:getOtherPlayers(player), function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择一起失去体力的角色，然后重置技能",
                cancelable = true,
            })[1]
            if to then
                room:loseHp(to, 1, que_linghu.name)
                room:loseHp(player, 1, que_linghu.name)
                player:setSkillUseHistory("que_linghu", 0, Player.HistoryPhase)
            end
        end
        if x > 0 then
            local targets = table.filter(room.alive_players, function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择摸牌的角色",
                cancelable = true,
            })[1]
            if to then
                to:drawCards(1, que_linghu.name)
            end
            local targets2 = table.filter(room.alive_players, function(p)
                return #p:getCardIds("hej") > 0
            end)
            local to2 = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets2,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择被弃牌的角色",
                cancelable = true,
            })[1]
            if to2 then
                local cid = room:askToChooseCard(player, {
                    target = to2,
                    flag = "hej",
                    skill_name = que_linghu.name,
                })
                room:throwCard(cid, que_linghu.name, to2, player)
            end
        end
        if x > 1 then
            local targets = table.filter(room.alive_players, function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择失去体力的角色",
                cancelable = true,
            })[1]
            if to then
                room:loseHp(to, 1, que_linghu.name)
            end
            local to2 = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择回复体力的角色",
                cancelable = true,
            })[1]
            if to2 then
                if to2:isWounded() then
                    room:recover {
                        who = to2,
                        num = 1,
                        recoverBy = player,
                        skillName = que_linghu.name
                    }
                end
            end
        end
        if x > 2 then
            local targets = table.filter(room.alive_players, function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择减少体力上限的角色",
                cancelable = true,
            })[1]
            if to then
                room:changeMaxHp(to, -1)
            end
            local to2 = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_linghu.name,
                prompt = "灵狐：请选择增加体力上限的角色",
                cancelable = true,
            })[1]
            if to2 then
                room:changeMaxHp(to2, 1)
            end
        end
        if x > 3 then
            local generals, same_generals = {}, {}
            for _, general_name in ipairs(room.general_pile) do
                same_generals = Fk:getSameGenerals(general_name)
                table.insert(same_generals, general_name)
                same_generals = table.filter(same_generals, function(g_name)
                    local general = Fk.generals[g_name]
                    return general.kingdom == "que" or general.subkingdom == "que" and general.name ~= "que__xuanyin"
                end)
                if #same_generals > 0 then
                    table.insert(generals, table.random(same_generals))
                end
            end
            if #generals == 0 then return false end
            generals = table.random(generals, 3)

            local skills = {}
            for _, general_name in ipairs(generals) do
                local general = Fk.generals[general_name]
                local general_skills = {}
                for _, s in ipairs(general:getSkillNameList()) do
                    local skill = Fk.skills[s]
                    if not (skill:hasTag(Skill.AttachedKingdom) and not table.contains(skill:getSkeleton().attached_kingdom, player.kingdom)) then
                        table.insertIfNeed(general_skills, s)
                    end
                end
                table.insertIfNeed(skills, general_skills)
            end
            if #skills > 0 then
                local choices = U.askToChooseGeneralSkills(player, {
                    generals = generals,
                    skills = skills,
                    min_num = 1,
                    max_num = 1,
                    skill_name = que_linghu.name,
                    prompt = "灵狐：请选择要获得的技能",
                    cancelable = false
                })
                room:handleAddLoseSkills(player, table.concat(choices, "|"))
            end
        end
        if x > 0 and not player.dead then
            player:drawCards(math.min(x, 3), que_linghu.name)
        end
        room:setPlayerMark(player, "@que_linghu-turn", 0)
    end,
})
return que_linghu
